<!--
To change this template, choose Tools | Templates
and open the template in the editor.
Created on : 2009-3-31
Author     : hq
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>Correlation Analysis</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <h2>Correlation analysis of multivariable</h2>
        <h3>Data upload:</h3>
        <form name="dataupload" action=# method="POST" enctype="multipart/form-data">
            <input type="file" name="data" value="" /><input type="submit" value="submit" name="submit" />
        </form>
        <%
        dhtml<-NULL
        if(!is.null(FILES$data$name)){
        fn <- paste(as.integer(Sys.time()), FILES$data$name, sep="")
        destination <- file.path('/var/www/images', fn) #tmp dir
        file.copy(FILES$data$tmp_name, destination, overwrite=TRUE)
        data<-read.csv(file=FILES$data$tmp_name)
        if(dim(data)[2]>1){
        rownames(data)<-as.character(data[,1])
        data<-data[,-1]
        fa<-sapply(1:dim(data)[2], function(x)is.factor(data[1,x]))
        if(TRUE %in% fa){
        cat("<font color='#FF0000'>There exist character values in your data!</font><br>\n")
        }else{
        library(R2HTML)
        datah<-paste(destination, ".html", sep="")
        dhtml<-paste(fn, ".html", sep="")
        HTML(data, file=datah)
        coo<-paste(fn, destination, sep="|")
        setCookie("coo", coo)
        }
        }else{
        cat("<font color='#FF0000'>Pls upload file with comma separated values!</font><br>\n")
        }
        }        
        %>
        Only data file in csv format is accepted.
        <a href="images/<%=dhtml%>" target="_blank">check</a>
        <h3>Correlation:</h3>
        Choose methods for missing values and correlation coefficient:
        <form name="cor" action=# method="POST">
            <select name="use">
                <option value="everything">everything</option>
                <option value="all.obs">all.obs</option>
                <option value="complete.obs">complete.obs</option>
                <option value="na.or.complete">na.or.complete</option>
                <option value="pairwise.complete.obs">pairwise.complete.obs</option>
            </select>
            <select name="method">
                <option value="pearson">pearson</option>
                <option value="kendall">kendall</option>
                <option value="spearman">spearman</option>
            </select>
            <input type="submit" value="Run"/>
        </form>
        <%
        dchtml<-NULL
        dccsv<-NULL
        dcpng<-NULL
        dna<-FALSE
        if(!is.null(POST$method) && !is.null(COOKIES$"coo")){
        fn<-unlist(strsplit(COOKIES$"coo", "|", fixed=TRUE))[1]
        destination<-unlist(strsplit(COOKIES$"coo", "|", fixed=TRUE))[2]
        data<-read.csv(file=destination)
        rownames(data)<-as.character(data[,1])
        data<-data[,-1]
        if(is.na(sum(data)) && POST$use=="everything"){
        cat("<font color='#FF0000'>Pls choose method for missing values!</font><br>\n")
        }else{
        dc<-cor(data, use=POST$use, method=POST$method)
        dcc<-paste(destination, "cc.csv", sep="")
        dccsv<-paste(fn, "cc.csv", sep="")
        write.csv(dc, file=dcc)
        library(R2HTML)
        dch<-paste(destination, "cc.html", sep="")
        dchtml<-paste(fn, "cc.html", sep="")
        HTML(dc, file=dch)
        dcpng<-paste(fn, "cc.png", sep="")
        dcp<-file.path("/var/www/images/R", dcpng)
        png(filename=dcp, width=600, height=600)
        if(!is.na(sum(dc))){
        heatmap(dc, symm=TRUE)
        dna<-FALSE
        }else{
        T <- !is.na(dc[1,])
        dna <- TRUE
        dc<-dc[T,T]
        heatmap(dc, symm=TRUE)
        }
        dev.off()
        }
        }
        %>
        <a href="images/<%=dchtml%>" target="_blank">Results</a> and download(<a href="images/<%=dccsv%>" target="_blank">csv</a>).
        <h3>Heatmap:</h3>
        <%
        if(dna){
        cat("<font color='#FF0000'>There exist NA values in correlation matrix!</font><br>\n")
        }
        %>
        <img src="/images/R/<%=dcpng%>" alt="heatmap according to correlation coeffient matrix."/>
    </body>
</html>
